package com.example.shopping.controller;

import com.example.shopping.pojo.Admin;
import com.example.shopping.pojo.User;
import com.example.shopping.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
public class BaseController {
    @Autowired
    private UserService userService;

    /**
     * 前往管理员登录
     * @return
     */
    @GetMapping("/adminLogin")
    public String toAdminLogin(){
        return "/adminLogin";
    }

    /**
     * 前往管理员登录验证
     */
    @PostMapping("/doAdminLogin")
    public String doAdminLogin(Admin admin){

        if (admin!=null){
            if (admin.getAccount().equals("admin")){
                return "indexAdmin";
            }
        }
        return "adminLogin";
    }
    /**
     * 前往用户登录
     */
    @GetMapping("/doUserLogin")
    public String doUserLogin(){
        return "userLogin";
    }
    /**
     * 前往个人中心
     */
    @GetMapping("/toPersonalCenter")
    public String toPersonalCenter(Model model, HttpSession session){

        return "personalCenter";
    }
    /**
     * 更新session
     */
    @GetMapping("/refreshSession/{id}")
    @ResponseBody
    public void refreshSession(HttpSession session, @PathVariable Integer id){
        session.setAttribute("user",userService.selectUserById(id));

    }
    @GetMapping("/adminLogOut")
    @ResponseBody
    public void adminLogOut(HttpSession session){
        session.removeAttribute("admin");
    }
}
